<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Tool extends MY_Controller {

	function __construct() {
		parent::__construct();
		//$this->load->model('u_user', 'user_m');
		$this->load->helper('file');
	}
	
	function generate_model($table_name = '') {
		//echo '<pre>';
		
		$this->load->database();
		if ($table_name == '') {
			$tables = $this->db->list_tables();
		} else {
			if ($this->db->table_exists($table_name)) {
				$tables[0] = $table_name;
			} else {
				echo 'Wrong table name';
				return;
			}
		}
		
		//print_r($tables);
		foreach ($tables as $table_name) {
			$data['class_name'] = ucfirst($table_name);
			$data['file_name'] = $table_name . '.php';
			$data['exec_class'] = __CLASS__;
			$data['exec_function'] = __FUNCTION__;
			$this->load->view('code_templates\model_template.tpl', $data);
			
			$output_stream = '<?php if (! defined ( \'BASEPATH\' )) exit ( \'No direct script access allowed\' );' . "\n" . $this->output->get_output();
			$this->output->set_output(NULL);
			
			$write_path = dirname(dirname(__FILE__)) . '\models\\' . $data['file_name'];
			
			if (file_exists($write_path)) {
				copy($write_path, $write_path . '.bak');
				echo 'File ' . $data['file_name'] . ' Backup!<br />';
			}
			
			if (!write_file($write_path, $output_stream)) {
				echo 'Unable to write the file<br />';
			} else {
				echo 'File written!<br />';
			}
		}
	}
	
	function generate_sel_airport_json() {
		$this->load->model('f_flight_scheduler_date', 'scheduler_date_m');
		$this->load->model('f_flight_scheduler', 'scheduler_m');
		$this->load->model('const_airport', 'const_airport_m');
		$this->load->model('const_area', 'const_area_m');
		
		$t = $this->scheduler_date_m->search_by_condition(array(), TRUE);
		$t1 = $this->scheduler_m->get_all_from_to_airport($t['id']);
		
		foreach ($t1 as $k => $v) {
			$t2[$k]['from_airport'] = $this->const_airport_m->get_by_id($v['fa_id']);
			$t2[$k]['from_airport']['area'] = $this->const_area_m->get_by_id($t2[$k]['from_airport']['area_id']);
			
			$i = 0;
			foreach ($t1 as $v1) {
				if ($v['fa_id'] == $v1['fa_id']) {
					$t2[$k]['to_airport'][$i] = $this->const_airport_m->get_by_id($v1['ta_id']);
					$t2[$k]['to_airport'][$i]['area'] = $this->const_area_m->get_by_id($t2[$k]['to_airport'][$i]['area_id']);
					$i ++;
				}
			}
		}
		
		$t3 = json_encode_jsfunc(super_unique($t2));
		//echo '<pre>';
		//print_r($t3);
		
		//return;
		$write_path = dirname(dirname(dirname(__FILE__))) . '\js\sel_airport_json.js';
		$write_str = 'var sel_airport_json = \'' . $t3 . '\'';
		
		if (file_exists($write_path)) {
			copy($write_path, $write_path . '.bak');
			echo 'File sel_airport_json.js Backup!<br />';
		}
		
		if (!write_file($write_path, $write_str)) {
			echo 'Unable to write the file<br />';
		} else {
			echo 'File written!<br />';
		}
	}
	
	function generate_flights_by_scheduler_id($scheduler_id_array = array()) {
		
		for ($i = 161; $i <= 183; $i++) {
			$scheduler_id_array[] = $i;
		}
		
		echo '<pre>';
		print_r($scheduler_id_array);
		return;
		
		$this->load->model('f_flight_scheduler_date', 'scheduler_date_m');
		$this->load->model('f_flight_scheduler', 'scheduler_m');
		$this->load->model('const_airplane_model', 'airplane_model_m');
		$this->load->model('f_flight', 'flight_m');
		
		foreach ($scheduler_id_array as $scheduler_id) {
			$t = $this->scheduler_m->get_by_id($scheduler_id);
			$t1 = $this->scheduler_date_m->get_by_id($t['scheduler_date_id']);
			$t2 = $this->airplane_model_m->get_by_id($t['airplane_model_id']);
			
			$begin = strtotime($t1['start_date']);
			$end = strtotime($t1['end_date']);
			
			//$k = 0;
			for($i = $begin; $i <= $end; $i += (24*3600)) {
				if (in_array(date('N', $i), str_split(str_replace('-', '', $t['scheduler'])))) {
					//echo date('Y-m-d', $i) . '<br />';
					$t3['flight_scheduler_id'] = $scheduler_id;
					$t3['departure_date'] = date('Y-m-d', $i);
					$t3['economy_class_remain_seats'] = $t2['economy_class_seats'];
					$t3['prestige_class_remain_seats'] = $t2['prestige_class_seats'];
			
					$this->flight_m->save($t3);
					//$k ++;
					//echo $k . '<br />';
				}
			}
		}
	}
}